package com.example.yimai.controller;

import com.example.yimai.dto.OrderRequestDTO;
import com.example.yimai.dto.PageResponseDTO;
import com.example.yimai.entity.Order;
import com.example.yimai.entity.OrderDetail;
import com.example.yimai.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param token 请求头携带的token
     * @param request 订单请求信息
     *                - address 收货地址
     *                - products 订单详情信息
     *                 - productId 商品id
     *                 - quantity 该商品数量
     * @return
     */
    @RequestMapping("/create")
    public Map<String, Object> createOrder(@RequestHeader("Authorization") String token,
                                           @RequestBody Map<String, Object> request) {
        // 从请求体中提取参数
        String address = (String) request.get("address");
        List<Map<String, Object>> productsData = (List<Map<String, Object>>) request.get("products");
        
        // 转换为OrderRequestDTO列表
        List<OrderRequestDTO> products = productsData.stream().map(item -> {
            OrderRequestDTO dto = new OrderRequestDTO();
            dto.setProductId(String.valueOf(item.get("productId")));
            dto.setQuantity(String.valueOf(item.get("quantity")));
            return dto;
        }).collect(java.util.stream.Collectors.toList());
        
        String result = orderService.createOrder(token, products, address);
        
        // 构造标准响应格式
        Map<String, Object> response = new HashMap<>();
        if (result.startsWith("success:")) {
            response.put("status", 200);
            response.put("message", "订单创建成功");
            // 提取订单号
            String serialNumber = result.split("success: ")[1];
            Map<String, String> data = new HashMap<>();
            data.put("serialNumber", serialNumber);
            response.put("data", data);
        } else {
            response.put("status", 500);
            response.put("message", result);
        }
        
        return response;
    }


    @RequestMapping("/queryOrder")
    public PageResponseDTO<Order> queryOrder(@RequestHeader("Authorization") String token,
                                             @RequestParam(required = false) String keyword,
                                             @RequestParam(defaultValue = "1") int pageNum,
                                             @RequestParam(defaultValue = "10") int pageSize){
        return orderService.queryOrder(token, keyword, pageNum-1, pageSize);
    }

    @RequestMapping("/getDetails/{orderId}")
    public List<OrderDetail> getDetails(@PathVariable String orderId){
        return orderService.getDetails(orderId);
    }

    @RequestMapping("/cancel/{serialNumber}")
    public String cancel(@PathVariable String serialNumber){
        return orderService.cancel(serialNumber);
    }

    @RequestMapping("/pay/{serialNumber}")
    public String pay(@PathVariable String serialNumber){
        return orderService.pay(serialNumber);
    }
    
}